package com.businessportal.web.controllers.authenticate;

import org.json.simple.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Vadim on 9/19/14.
 */
@WebServlet(urlPatterns = "/securepages/logout.do")
public class LogoutController  extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json");

        JSONObject retVal=new JSONObject();

        try
        {
            HttpSession session = req.getSession(false);

            if(session != null){
                session.removeAttribute("user");
                session.invalidate();
            }
            retVal.put("message", "OK");
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            retVal.put("error", e.getMessage());
        }

        resp.getWriter().write(retVal.toString());
    }
}
